
#include "fb_camera.h"

#include "graphics/graphics.h"

#include "util/common_macros.h"		// macros
#include "util/log.h"		// macros
#include "util/mmgr.h"		// Needs to come in early

#include <cassert>

using namespace camera;

FbCamera ::FbCamera ( const math::Vec3& _From,const math::Vec3& _To,const math::Vec3& _Up,
                               float _FOV, float _Aspect, const math::Vec2& _ClipRange):
        AbstractCamera( _From,_To,_Up,_FOV, _Aspect, _ClipRange)
{
    m_Width   = GetGraphics()->GetScreenWidth();
    m_Height  = GetGraphics()->GetScreenHeight();
    SetProjection(m_FOV, (float)m_Width / (float)m_Height, _ClipRange);

    m_Stencil = true;
    m_ZBuffer = true;
}

FbCamera ::~FbCamera ()
{
}

bool FbCamera::StartFrame(bool _Clear)
{
    // Set the matrices
	if (!GetGraphics()->BeginScene()) return false;	// probably in screensaver mode

	GetGraphics()->SetTransform(Graphics::TS_VIEW, m_World);
	GetGraphics()->SetTransform(Graphics::TS_PROJECTION, m_Projection);

    if (m_pViewPort)
    {   
		GetGraphics()->SetViewport(*m_pViewPort);
    }

	Clear(_Clear, _Clear, _Clear);

	s_pCurrentCamera = this;
    return true;
}


bool FbCamera::EndFrame()
{
	GetGraphics()->EndScene();

    s_pCurrentCamera = NULL;        // now is free again
    return true;
}

bool FbCamera::Show()
{
	return GetGraphics()->Present();
}
